package br.ufmg.ubicomp.droidguide.eventservice;

import java.util.Calendar;
import java.util.logging.Level;

import br.ufmg.ubicomp.droidguide.common.DroidGuideConfigurationManager;
import br.ufmg.ubicomp.droidguide.common.DroidGuideConstants;
import br.ufmg.ubicomp.droidguide.communication.EventServiceCommunication;
import br.ufmg.ubicomp.droidguide.eventservice.event.UserEvent;
import br.ufmg.ubicomp.droidguide.eventservice.management.EventManager;
import br.ufmg.ubicomp.droidguide.eventservice.notification.DGNotificationType;
import br.ufmg.ubicomp.droidguide.eventservice.notification.NotificationBucket;
import br.ufmg.ubicomp.droidguide.utils.AndroidUtils;
import br.ufmg.ubicomp.droidguide.utils.DateUtil;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class UserEventProcessingService extends Service {

	private Handler mHandler = new Handler();
	private long startTime;
	
	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		Log.d("UserEventProcessingService", "onStart");
		startTime = System.currentTimeMillis();
		
//		sendEventsToServer();
		AndroidUtils.showAlert(getApplicationContext(), "Starting event processing service . . .");

		Thread task = new Thread(){
        	@Override
        	public void run() {
        		
//        		Intent intent = new Intent(DroidGuideConstants.USER_EVENT_BROADCAST_RECEIVER);
//        		sendBroadcast(intent);
//        		
//        		AndroidUtils.runTask(this, DroidGuideConstants.USER_EVENTS_UPDATE_INTERVAL);        		
        	}
        };
		
		//mHandler.postDelayed(task, 0);
	}
	
	private void sendEventsToServer() {
		try {
			AndroidUtils.showAlert(this, "Updating events . . .");
			int i = sendUserEventsToServer();
			NotificationBucket.getInstance().addNotification(
					DGNotificationType.APPLICATION, 
					System.currentTimeMillis(),
					DateUtil.formatDate(Calendar.getInstance(), "hh:mm:ss") + 
					": " + i + " events to server", "UserEvents", Level.FINEST);
		} catch (Exception e) {
			e.printStackTrace();
			AndroidUtils.notifyError(e, getApplicationContext(), "SendEventsToServer", "UserEventProcessingService");
		}
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public int sendUserEventsToServer() throws Exception
	{
		EventManager man = EventManager.getInstance();

		String dgKey = DroidGuideConfigurationManager.getInstance().getData(
				DroidGuideConstants.DROID_GUIDE_USER_KEY);
		int i = 0;
		for (UserEvent evt: man.getUserEvents())
		{
			// if the event has not been sent to server
			if (!evt.isSentToServer())
			{
				EventServiceCommunication.save(dgKey, 
					evt.getType().getDescription(), 
					evt.getSource().getDescription(), evt.getData());
				evt.setSentToServer(true);
				i++;
			}
		}
		
		if (DroidGuideConstants.USER_EVENT_STRATEGY_REMOVE_ON_SAVE)
		{
			man.clearEvents();
		}
		return i;
	}
	
}
